<%@ page isELIgnored="false" language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fun" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<c:set var="_path" value="${pageContext.request.contextPath}"></c:set>
<style type="text/css">
</style>
<script type="text/javascript">
	function addOrUpdateUser(){
		var r = $('#roleForm').form('validate');
		if(!r) {
			return false;
		}
		var obj = $("#roleForm").serializeArray();
		var sel = $('#resourceTree').tree('getChecked');
		var pndes=$('#resourceTree').find('.tree-checkbox2').parent();
		for(var i = 0 ; i < pndes.length ; i++){
			var o = {
					name : 'resid',
					value: '' + $(pndes[i]).attr('node-id')
				};
			obj.push(o);
		}
		for(var i = 0;i < sel.length;i++){
			var o = {
				name : 'resid',
				value: '' + sel[i].id
			};
			obj.push(o);
		}
		/*
		var selRes = {};
		selRes.name = "resource";
		selRes.value = sel;
		obj.push(selRes);
		console.log(sel);
		console.log(obj);
		*/
		$.post("role/saveResources",obj,function(data){
			if(data.result == 'succ'){
				$('#MyPopWindow').window('close');
				$('#roleTable').datagrid('reload');  
				$.messager.alert('提示','保存成功','info');
			}else{
				if(data.reason == 'exists'){
					$.messager.alert('提示','已经存在','info');
				}else{
					$.messager.alert('提示','保存失败','info');
				}
			}
		});
	}
</script>
<form id="roleForm" method="post" style="margin: 10;text-align: center;">
<input type="hidden" name="roleid" value="${roleid }"/>
<table >
	<tr>
		<td>
			<div id="resourceTree" ></div>
			
		</td>
	</tr>
</table>
<a href="#" id="btn-back" onclick="closeWindow();" class="easyui-linkbutton" iconCls="icon-back">返回</a>
<a href="#" id="btn-add" onclick="addOrUpdateUser();" class="easyui-linkbutton" iconCls="icon-save">保存</a>
</form>
<script>
$('#resourceTree').tree({
	url:'${_path }/role/loadResources?roleid=${roleid}',
	checkbox:true,
	animate:true
});
</script>